package db

import (
	"fmt"

	"gorm.io/gorm"
)

// 用户模型
type User struct {
	gorm.Model
	Name     string `json:"name"`
	Email    string `json:"email" gorm:"unique"`
	Password string `json:"password"`
	IsStop   bool   `json:"isStop" gorm:"column:is_stop;default:false"`
}

func GetUserByEmail(email string) (User, error) {
	var user User
	err := DB.Where("email = ?", email).First(&user).Error
	return user, err
}

// 添加用户
//
// 内部判断用户是否存在
func AddUser(user *User) (uint, error) {
	user1 := User{}
	DB.First(&user1).Where("email = ?", user.Email)
	if user1.ID != 0 {
		return 0, fmt.Errorf("user [%v] is existed. ", user.Email)
	}
	err := DB.Create(&user).Error
	return user.ID, err
}
